package cn.fanyamin.business;



import java.util.Iterator;
import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.fanyamin.dao.ItemDao;
import cn.fanyamin.dao.ModuleDao;
import cn.fanyamin.dao.TaskDao;
import cn.fanyamin.dao.UserDao;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Module;
import cn.fanyamin.domain.Remind;
import cn.fanyamin.domain.RepeatInfo;
import cn.fanyamin.domain.Task;
import cn.fanyamin.domain.User;
import cn.fanyamin.util.collection.CollectionUtils;
import cn.fanyamin.util.email.EmailSender;


/**
 * @author walter
 *
 */
public class DWRService {
    private static final Log logger = LogFactory.getLog(DWRService.class);

    private UserDao userDao;
  
    private ModuleDao moduleDao;
    
    private TaskDao taskDao;
    
    private ItemDao itemDao;
    
    public DWRService() {
        

    }
    
    public List<UserCategory> getCategoryList(int categoryType, int userID) {
    	return itemDao.getCategoryList(categoryType, userID);
    }
    
    public List<Task> getSubTaskList(int taskID) {
    	return taskDao.getSubTaskList(taskID);
    }
    
    public int getSubTaskCount(int taskID) {
    	return taskDao.getSubTaskCount(taskID);
    }
    
    public int isUserExisted(User user){
        List<User> userList = userDao.find(user);
        if (CollectionUtils.isEmpty(userList)){
            return 0;
        }
        return 1;
    }
    
    public List<Module> getModuleList(int roleID){
    	List<Module> moduleList = moduleDao.getModuleList(roleID);
    	Iterator<Module> it = moduleList.listIterator();
    	while(it.hasNext()) {
    		Module mo = it.next();
    		if(mo.getModuleStatus()<=0) {
    			it.remove();
    		}
    	}
        return moduleList;
    }
    
    public List<Module> getFavoriteModules(User aUser, Module aModule){
    	
    	int parentModuleID = aModule.getParentModuleID();
    	if(parentModuleID == 0) {
    		parentModuleID = aModule.getModuleID();
    	}
    	List<Module> moduleList = aUser.getRole().getModuleList();
    	List<Module> retList = null;
    	for(Module mo: moduleList) {
    		if(mo.getModuleID() == parentModuleID) {
    			if( mo.hasSubModule()) {
    				retList = mo.getSubModuleList();
    				Iterator<Module> it = retList.listIterator();
    		    	while(it.hasNext()) {
    		    		Module mi = it.next();
    		    		if(mi.getModuleStatus()<=0) {
    		    			it.remove();
    		    		}
    		    	}
    		    	return retList;
    			}	
    		}
    	}
        return moduleDao.getFavoriteModules(aUser.getUserID());
    }
    
    public RepeatInfo getRepeatInfo(int repeatID) {
        return taskDao.retrieveRepeatInfo(repeatID);
    }
    
    public Remind getRemind(int remindID) {
        return taskDao.retrieveRemind(remindID);
    }
    
    /**
     * @param userDao the userDao to set
     */
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * @param moduleDao the moduleDao to set
     */
    public void setModuleDao(ModuleDao moduleDao) {
        this.moduleDao = moduleDao;
    }

   
    /**
     * @param taskDao the taskDao to set
     */
    public void setTaskDao(TaskDao taskDao) {
        this.taskDao = taskDao;
    }



    
    /**
     * @param sender the sender to set
     */
    public void setItemDao(ItemDao itemDao) {
        this.itemDao = itemDao;
    }
}
